package org.lex.ftp.ui.speed;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import org.lex.ftp.util.StatusBar;
import org.lex.net.ftp.FTPURL;
import org.lex.utils.text.Splitter;


public class SpeedPane extends JPanel implements StatusBar {
	private JScrollPane scrollPane = null;
	private JTable table = null;
	private JLabel statusLabel = null;
	private TablePopupMenu tablePopupMenu = null;
	SpeedTableModel tableModel = null;

	public SpeedPane() {
		super();
		initialize();
	}

	public void parseInput(String input) {
		this.tableModel.clearAll();
		String[] lines = Splitter.LineSplitter.split(input);
		for (int i = 0; i < lines.length; i++) {
			FTPURL ftpUrl = FTPURL.parseFtpUrl(lines[i]);
			if (null != ftpUrl) {
				this.tableModel.addServer(ftpUrl, ServerStatus.Unkown, 0L);
			}
		}
		this.tableModel.fireTableDataChanged();
	}

	public void setStatus(String text) {
		this.statusLabel.setText(text);
	}

	@Override
	public void updateUI() {
		super.updateUI();
		this.getTablePopupMenu().updateUI();
	}

	FTPURL getSelectedUrl() {
		int row = this.getTable().getSelectedRow();
		if (-1 == row)
			return null;
		return this.getTableModel().getRow(row);
	}

	// =======================
	private void initialize() {
		statusLabel = new JLabel();
		statusLabel.setBorder(BorderFactory.createEtchedBorder());
		statusLabel.setHorizontalAlignment(SwingConstants.CENTER);
		this.setLayout(new BorderLayout());
		this.add(new ToolBar(this), BorderLayout.NORTH);
		this.add(getScrollPane(), BorderLayout.CENTER);
		this.add(statusLabel, BorderLayout.SOUTH);
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTable());
			scrollPane.getViewport().setBackground(Color.white);
		}
		return scrollPane;
	}

	private TablePopupMenu getTablePopupMenu() {
		if (null == tablePopupMenu) {
			tablePopupMenu = new TablePopupMenu(this);
		}
		return tablePopupMenu;
	}

	JTable getTable() {
		if (table == null) {
			table = new JTable() {
				/**
				 * Below class is used to eliminate the grey area of the scroll
				 * pane that is not filled in the table.
				 */
				@Override
				public boolean getScrollableTracksViewportHeight() {
					Component parent = getParent();

					if (parent instanceof JViewport) {
						return parent.getHeight() > getPreferredSize().height;
					}

					return false;
				}
			};
			table.setModel(this.getTableModel());
			table.setComponentPopupMenu(this.getTablePopupMenu());
			table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			int[] widths = { 235, 100, 75, 150 };
			TableColumnModel model = table.getColumnModel();
			TableCellRenderer renderer = new SpeedTableRenderer();
			for (int i = 0; i < model.getColumnCount(); i++) {
				TableColumn column = model.getColumn(i);
				column.setPreferredWidth(widths[i]);
				column.setCellRenderer(renderer);
			}
		}
		return table;
	}

	SpeedTableModel getTableModel() {
		if (null == this.tableModel)
			this.tableModel = new SpeedTableModel();
		return this.tableModel;
	}
}
